/*
    XTU OJ: 1476
    by: fifth_light
*/

#include<stdio.h>
#include<stdint.h>
#include<inttypes.h>

int64_t max_number(int radix) {
    return 6 * radix * radix + 6 * radix + 6;
}

int calc(int64_t input, int radix) {
    int combo = 0;
    int64_t now = 1;
    while (input >= now) {
        int digit = input / now % radix;
        if (digit == 6) {
            combo++;
            if (combo == 3) {
                return 1;
            }
        } else {
            combo = 0;
        }
        now *= radix;
    }
    return 0;
}

int main() {
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
        int64_t n;
        scanf("%" SCNd64, &n);
        int max, first = 1;
        for (int i = 6; n >= (max = max_number(i)); i++) {
            if (n == max) {
                if (first) {
                    printf("%d", i);
                    first = 0;
                } else {
                    printf(" %d", i);
                }
                goto end;
            }
            if (calc(n, i)) {
                if (first) {
                    printf("%d", i);
                    first = 0;
                } else {
                    printf(" %d", i);
                }
            }
        }
        if (first) {
            puts("-1");
            continue;
        }
end:
    puts("");
        ;
    }
    return 0;
}
